package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.utlis.Aes;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.user.MchUserBO;
import com.ymk.core.code.cmd.UserCmdCode;
import com.ymk.core.enums.mch.MchUserStatusEnum;
import com.ymk.service.IMchUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Service
public class MchUserPostCmd extends ACmd {

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        MchUserBO mchUserBO = super.getData(sign, MchUserBO.class);

        String salt = Aes.creatSalt();
        mchUserBO.setSalt(salt);
        mchUserBO.setPassword(Aes.getMD5(mchUserBO.getPassword(), salt));
        mchUserBO.setStatus(MchUserStatusEnum.NORMAL.getStatus());
        super.setCreatedData(sign,mchUserBO);

        this.mchUserService.post(mchUserBO);
        return respSign;
    }

    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {
        MchUserBO reqData = super.getData(sign, MchUserBO.class);
        if (StringUtils.isBlank(reqData.getMobile())){
            throw new MallException("手机号为空");
        }
        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("mobile", reqData.getMobile());
        MchUserBO mchUserBO = this.mchUserService.get(paramsBuilder.build());
        if (Objects.nonNull(mchUserBO)){
            throw new MallException("手机号已存在");
        }

        return null;
    }


    @Override
    public String getCmdCode() {
        return UserCmdCode.MCH_USER_POST;
    }

    @Autowired
    private IMchUserService mchUserService;
}